(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.addrViewController', addrViewController);
	addrViewController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', 'ajaxServiceFactory' ];
	function addrViewController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, ajaxServiceFactory) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("companyAddrViewPopup"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getCountryOptions : {
				url : 'do/cda/dataList/country',
				method : 'get'
			},
			getCompanyAddrTypeOptions : {
				url : 'do/cda/dataList/companyAddrTypes',
				method : 'get'
			},
			getStates : {
				url : 'do/cda/dataList/states',
				method : 'post'
			},
			getCities : {
				url : 'do/cda/dataList/cities',
				method : 'post'
			},
		});
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.companyAddr = {};
		ctrl.companyAddrOld = {};
		ctrl.formName = "companyAddrForm";
		
		ctrl.init = function() {
			initOptions(ctrl).then(function() {
				ctrl.companyAddr = ctrl.dialogsModule.params.companyAddr || {};
				ctrl.companyAddrOld = ctrl.dialogsModule.params.companyAddrOld;
			});
		};
		
		function initOptions(ctrl){
			var defer1 = $q.defer();
			var defer2 = $q.defer();
			ajaxService.getCountryOptions().then(function(response) {
				angular.extend(ctrl.companyCountryOptions, response);
				defer1.resolve();
			}, angular.noop);
			ajaxService.getCompanyAddrTypeOptions().then(function(response) {
				ctrl.companyAddrTypeOptions = response;
				defer2.resolve();
			}, angular.noop);
			return $q.all({
				defer1 : defer1.promise,
				defer2 : defer2.promise
			});
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
	}
})(angular);